package com.my.lucky.controller;

import com.my.lucky.dto.GaiNianDTO;
import com.my.lucky.service.GaiNianInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Slf4j
@Controller
@RequestMapping("/gainian")
public class GaiNianInfoController {

    @Autowired
    private GaiNianInfoService service;

    /**
     * 概念复盘
     */
    @GetMapping
    public String gainianPage(Model model) {
        LocalDate today = LocalDate.now();
        LocalDate yesterday = LocalDate.now().minusDays(1);//默认取2天数据
        if (today.getDayOfWeek() == DayOfWeek.MONDAY) {//周一取到周五的数据
            yesterday = today.with(DayOfWeek.FRIDAY).minusWeeks(1);
        } else if (today.getDayOfWeek() == DayOfWeek.SATURDAY || today.getDayOfWeek() == DayOfWeek.SUNDAY) {
            yesterday = today.with(DayOfWeek.THURSDAY);//周末取到周四周五的数据
        }
        LocalDate endDate = today;
        model.addAttribute("startDate", yesterday);
        model.addAttribute("endDate", endDate);
        return "gainian";
    }

    @GetMapping("/summary")
    @ResponseBody
    public GaiNianDTO getSealOrderAmountSummary(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate) {
        LocalDateTime now = null;
//        System.out.println("getSealOrderAmountSummary==="+startDate);
        if (startDate != null) {
            now = LocalDateTime.of(startDate, LocalTime.MAX);
        }
        return service.getSealOrderAmountSummary(now);
    }

    /**
     * 分析个股的资金情况，支持通过code参数过滤并跳转到详情页
     *
     * @param code 股票代码，可选参数
     * @return 股票详情页面
     */
    @GetMapping("/summarycode")
    public String summarycode(Model model, @RequestParam(required = false) String code) {
        try {
            LocalDate endDate = LocalDate.now();
            LocalDate start = endDate.minusDays(60);

            // 验证日期有效性
            if (start.isAfter(endDate)) {
                model.addAttribute("error", "开始日期不能晚于结束日期");
                return "getGainian";
            }
        } catch (Exception e) {
            log.error("获取股票数据失败，code={}", code, e);
            model.addAttribute("error", "获取数据失败：" + e.getMessage());
        }
        return "getGainian";
    }

    @GetMapping("/summarycodeData")
    @ResponseBody
    public GaiNianDTO summarycodeData(@RequestParam(required = false) String code) {

        try {
            LocalDate endDate = LocalDate.now();
            LocalDate start = endDate.minusDays(60);
            GaiNianDTO gaiNianDTO = service.getSealOrderAmountSummaryCode(LocalDateTime.of(endDate, LocalTime.MAX), code);
            // 验证日期有效性
            if (start.isAfter(endDate)) {
                //    model.addAttribute("error", "开始日期不能晚于结束日期");
                return null;
            }
//            System.out.println("gaiNianDTO="+gaiNianDTO);
            return gaiNianDTO;
        } catch (Exception e) {
            log.error("获取股票数据失败，code={}", code, e);
        }
        return null;
    }
}